package com.sunriz.motordrive.server.photoThermal.Impl;

import com.sunriz.motordrive.domain.DTO.SetValueDTO;
import com.sunriz.motordrive.domain.DTO.MotorDebugDTO;
import com.sunriz.motordrive.domain.DTO.Result;
import com.sunriz.motordrive.server.photoThermal.Debug;
import com.sunriz.motordrive.server.common.SetValueServer;
import com.sunriz.motordrive.server.common.impl.BaseSerialImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * ClassName: DebugImpl
 * Package: com.sunriz.motordrive.server.impl
 * Description:
 *
 * @Author BuTang
 * @Create 2024/5/20 14:20
 * @Version 1.0
 */

@Slf4j
@Service
public class DebugImpl implements Debug {

    @Autowired
    SetValueServer setValue;
    @Autowired
    BaseSerialImpl serialPort;


    @Override
    public Result setMotorModel(MotorDebugDTO modelAndValue) {

        Integer integer = Integer.valueOf(modelAndValue.model);
        switch (integer){
            case 0:
                Result result1 = motorClosed();
                return result1;
            case 1:
                Result result = targetControl(modelAndValue);
                return result;
            // returnOrigin
            case 2:
                return returnOrigin(modelAndValue);
            // findOrigin
            case 3:
                return findOrigin(modelAndValue);
            default:
                return Result.fail("接口执行失败","403");
        }

    }

    /**
     * 关闭电机
     * @return Result
     */
    private Result motorClosed(){
        Result result = setValue.single(new SetValueDTO("1","103", "01"));
        if ( true == result.getSuccess()){
            return Result.success("电机关闭成功","200");
        }
        return Result.fail("电机关闭失败","403");
    }

    /**
     * 目标控制
     * @param modelAndValue 目标位置值
     * @return 错误信息
     */
    private Result targetControl(MotorDebugDTO modelAndValue){

        if (modelAndValue.value!=null && modelAndValue.value!=""){
            String value = modelAndValue.getValue();
            Integer integer = Integer.valueOf(value);
            int[] ints = new int[4];
            for (int i = 0; i < 4; i++) {
                // 取最低的8位
                ints[i] = integer & 0xFF;
                // 无符号右移8位
                integer >>>= 8;
            }

            Result result1 = motorClosed();
            Result result2 = serialPort.sendModbus(new byte[]{01, 06, 00, 105, 00, 01});
            Result result3 = serialPort.sendModbus(new byte[]{01, 16, 00, 112, 00, 02, 04, (byte) ints[3], (byte) ints[2], (byte) ints[1], (byte) ints[0]});
            Result result4 = serialPort.sendModbus(new byte[]{01, 06, 00, 102, 00, 01});
            if (true == result1.getSuccess() && true == result2.getSuccess() && true == result3.getSuccess() && true == result4.getSuccess()){
                return Result.success("电机执行位置控制成功","200");
            }
            return Result.fail("位置控制执行失败，请查看","403") ;
        }else {
            return Result.fail("使用位置控制时，目标位置不能为空！","403");
        }


    }

    private Result returnOrigin(MotorDebugDTO modelAndValue){
        Result result1 = motorClosed();
        Result result2 = serialPort.sendModbus(new byte[]{01, 06, 00, 104, 00, 01});
        Result result3 = serialPort.sendModbus(new byte[]{01, 06, 00, 102, 00, 01});
        return null ;
    }
    private Result findOrigin(MotorDebugDTO modelAndValue){
        Result result1 = motorClosed();
        Result result2 = serialPort.sendModbus(new byte[]{01, 06, 00, 107, 00, 01});
        Result result3 = serialPort.sendModbus(new byte[]{01, 16, 00, 110, 00, 02,04,00,00,11, (byte) 184});
        Result result4 = serialPort.sendModbus(new byte[]{01, 06, 00, 102, 00, 01});
        return null ;
    }



}
